<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="租户编号" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入租户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactUserName">
        <el-input
          v-model="queryParams.contactUserName"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:tenant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:tenant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tenant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:tenant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tenantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template #default="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="租户编号" align="center" prop="tenantId" :show-overflow-tooltip="true"/>
      <el-table-column label="联系人" align="center" prop="contactUserName" :show-overflow-tooltip="true"/>
      <el-table-column label="联系电话" align="center" prop="contactPhone" :show-overflow-tooltip="true"/>
      <el-table-column label="企业名称" align="center" prop="companyName" :show-overflow-tooltip="true"/>
      <el-table-column label="统一社会信用代码" align="center" prop="licenseNumber" :show-overflow-tooltip="true"/>
        <el-table-column label="过期时间" align="center" prop="expireTime"  width="160">
            <template #default="scope">
                <span>{{ parseTime(scope.row.expireTime) }}</span>
            </template>
        </el-table-column>
      <el-table-column label="租户状态" align="center" prop="status" :show-overflow-tooltip="true">
        <template #default="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-tooltip content="详情" placement="top">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['system:tenant:detail']"></el-button>
          </el-tooltip>
          <el-tooltip content="编辑" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:tenant:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="同步套餐" placement="top">
            <el-button link type="primary" icon="Refresh" @click="handleSyncTenantPackage(scope.row)" v-hasPermi="['system:tenant:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:tenant:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <Dialog ref="dialog" @refresh="getList"></Dialog>
  </div>
</template>

<script setup name="Tenant">
import { listTenant, delTenant, changeStatus, syncTenantPackage } from "@/api/system/tenant";
import Dialog from "@/views/system/tenant/dialog";

const { proxy } = getCurrentInstance();

const tenantList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    tenantId: null,
    contactUserName: null,
    contactPhone: null,
    companyName: null,
    remark: null,
  },
});

const { queryParams } = toRefs(data);

/** 查询租户列表 */
function getList() {
  loading.value = true;
  listTenant(queryParams.value).then(response => {
    tenantList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs["dialog"].show();
}

/** 详情按钮操作 */
function handleDetail(row) {
  proxy.$refs["dialog"].show(row.id, true);
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  proxy.$refs["dialog"].show(_id);
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除租户编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delTenant(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/tenant/export', {
    ...queryParams.value
  }, `租户_${new Date().getTime()}.xlsx`)
}

/** 同步租户套餐按钮操作 */
function handleSyncTenantPackage(row) {
  proxy.$modal.confirm('是否确认同步租户套餐租户编号为"' + row.tenantId + '"的数据项？').then(() => {
    loading.value = true;
    return syncTenantPackage(row.tenantId, row.packageId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("同步成功");
  }).catch(() => {
    loading.value = false;
  });
}

// 租户状态修改
function handleStatusChange(row) {
  let text = row.status === "0" ? "启用" : "停用";
  proxy.$modal.confirm('确认要"' + text + '""' + row.companyName + '"租户吗？').then(() => {
    loading.value = true;
    return changeStatus(row.id, row.tenantId, row.status)
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
    getList();
  }).catch(function() {
    row.status = row.status === "0" ? "1" : "0";
    loading.value = false;
  });
}

getList();
</script>
